package com.github.iot.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;

import com.github.iot.R;
import com.github.iot.dao.CommonDaoUtils;
import com.github.iot.dao.DaoUtilsStore;
import com.github.iot.entity.Button;
import com.github.iot.utils.BarUtils;
import com.github.iot.utils.FastJsonUtils;
import com.github.iot.utils.KeyboardUtils;
import com.github.iot.utils.ToastUtils;
import com.github.iot.view.RxTitle;
import com.github.iot.view.dialogs.fragment.SimpleDialogFragment;
import com.github.iot.view.dialogs.iface.ISimpleDialogListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Activity
 *
 * @Author: 梁声洪
 * @Date: 2019/12/29 9:14
 * @Version: v1.0.0
 */
public class ButtonActivity extends AppCompatActivity implements ISimpleDialogListener {

    private static final int REQUEST_SIMPLE_DIALOG = 1;

    @BindView(R.id.rx_title)
    RxTitle mTitle;

    @BindView(R.id.et_name)
    AppCompatEditText mName;

    @BindView(R.id.et_open)
    AppCompatEditText mOpen;

    @BindView(R.id.et_close)
    AppCompatEditText mClose;

    @BindView(R.id.et_sort)
    AppCompatEditText mSort;

    private static Button button;

    private static CommonDaoUtils<Button> buttonDaoUtils = DaoUtilsStore.getInstance().getButtonDaoUtils();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.setStatusBarColor(this, R.color.colorPrimaryDark);
        setContentView(R.layout.activity_button);

        ButterKnife.bind(this);
        button = FastJsonUtils.strToBean(getIntent().getStringExtra("button"), Button.class);

        initView();
        initData();
    }

    private void initView() {
        mTitle.setLeftFinish(this);
        mTitle.setTitle(button.getName());
        mName.setText(button.getName());

        if (button.getOpen() != null) {
            mOpen.setText(String.valueOf(button.getOpen()));
        }

        if (button.getClose() != null) {
            mClose.setText(String.valueOf(button.getClose()));
        }

        if (button.getSort() != null) {
            mSort.setText(String.valueOf(button.getSort()));
        }
    }

    private void initData() {

    }

    private void buttonOperation() {
        String name = mName.getText().toString();
        String open = mOpen.getText().toString();
        String close = mClose.getText().toString();
        String sort = mSort.getText().toString();

        if (TextUtils.isEmpty(name)) {
            ToastUtils.warning("柜门名称不能为空");
            return;
        }

        if (TextUtils.isEmpty(open)) {
            ToastUtils.warning("按钮开指令不能为空");
            return;
        }

        if (TextUtils.isEmpty(close)) {
            ToastUtils.warning("按钮关指令不能为空");
            return;
        }

        if (TextUtils.isEmpty(sort)) {
            ToastUtils.warning("排序不能为空");
            return;
        }

        button.setName(name);
        button.setOpen(Integer.valueOf(open));
        button.setClose(Integer.valueOf(close));
        button.setSort(Integer.valueOf(sort));

        boolean flag = buttonDaoUtils.update(button);
        if (flag) {
            ToastUtils.success("操作成功");
            finish();
        } else {
            ToastUtils.error("操作失败");
        }
    }

    @OnClick({R.id.btn_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_button:
                KeyboardUtils.hideSoftInput(ButtonActivity.this);
                SimpleDialogFragment.createBuilder(ButtonActivity.this, getSupportFragmentManager())
                        .setTitle("系统提示")
                        .setMessage("您确定要操作吗？")
                        .setPositiveButtonText("确定")
                        .setNegativeButtonText("取消")
                        .setRequestCode(REQUEST_SIMPLE_DIALOG)
                        .setCancelable(false)
                        .show();
                break;
            default:

                break;
        }
    }

    @Override
    public void onNegativeButtonClicked(int requestCode) {

    }

    @Override
    public void onNeutralButtonClicked(int requestCode) {

    }

    @Override
    public void onPositiveButtonClicked(int requestCode) {
        if (requestCode == REQUEST_SIMPLE_DIALOG) {
            buttonOperation();
        }
    }

}
